package com.dsj.gxrd.modules.activity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.constants.GxrdConstants;
import com.dsj.gxrd.modules.activity.domain.entity.RdScoreDetail;
import com.dsj.gxrd.modules.activity.domain.query.RdScoreDetailQuery;
import com.dsj.gxrd.modules.activity.domain.vo.RdScoreCockpitRankVo;
import com.dsj.gxrd.modules.activity.domain.vo.RdScoreDetailInfoVo;
import com.dsj.gxrd.modules.activity.domain.vo.RdScoreDetailVo;
import com.dsj.gxrd.modules.activity.mapper.RdScoreDetailMapper;
import com.dsj.gxrd.modules.activity.service.RdScoreDetailService;
import com.ruoyi.common.core.utils.Assert;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author songpin566
 * @date 2023/10/12
 **/
@Service
public class RdScoreDetailServiceServiceImpl extends ServiceImpl<RdScoreDetailMapper, RdScoreDetail> implements RdScoreDetailService {

    @Autowired
    private RdScoreDetailMapper scoreDetailMapper;

    @Override
    public List<RdScoreDetailVo> selectScoreDetailList(RdScoreDetailQuery scoreDetailQuery) {
        List<RdScoreDetailVo> list = scoreDetailMapper.selectScoreDetailList(scoreDetailQuery, GxrdConstants.SCORE_DETAILS_LEVELS);
        //设置所属级别名称
        setLevelNames(list, scoreDetailQuery.getScoreYear());
        return list;
    }

    @Override
    public AjaxResult getInfo(Long userId, Integer year) {
        Assert.isTrue(userId==null,"请先登录!");
        RdScoreDetailVo rn = scoreDetailMapper.selectScoreDetailRank(userId,GxrdConstants.SCORE_DETAILS_LEVELS,year);
        List<RdScoreDetailInfoVo> list = scoreDetailMapper.getInfo(userId, year);
        return new AjaxResult().put("rows",list).put("rn",rn);
    }

    @Override
    public List<RdScoreCockpitRankVo> rank(Integer year) {
        return scoreDetailMapper.rank(year);
    }


    private void setLevelNames(List<RdScoreDetailVo> list, Integer scoreYear) {
        StringBuilder s = new StringBuilder();
        for (RdScoreDetailVo rdScoreDetailVo : list) {
            rdScoreDetailVo.setScoreYear(scoreYear);
            String level = rdScoreDetailVo.getLevel();//0,1,2
            if (StringUtils.isNotEmpty(level)) {
                String[] split = level.split(",");
                for (String id : split) {
                    String name = GxrdConstants.DICT_TYPE_GXRD_LEVEL_MAP.get(id);
                    s.append(name).append(",");
                }
                s.deleteCharAt(s.length() - 1);
                rdScoreDetailVo.setLevelName(s.toString());
                s.delete(0, s.length());
            }
        }
    }


}
